package org.wu.reggie.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wu.reggie.entity.*;
import org.wu.reggie.mapper.*;
import org.wu.reggie.service.OrderService;
import org.wu.reggie.utils.UUIDUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 作用 下单
     *
     * @param orders 订单信息 地址id 支付方式 备注
     * @param userId 当前登录者
     */
    @Override
    @Transactional
    public void submit(Orders orders, Long userId) {

        // 1 获取用户的购物车记录
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findCartByUserId(userId);

        // 2 根据当前登录用户id 查询用户数据
        User user = userMapper.findById(userId);

        // 3 根据地址ID 查询地址数据
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());


        // 4 随机生成订单id 以及 记录总金额
        Long orderId = UUIDUtils.getUUIDOrderId().longValue();
        BigDecimal totalAmount = new BigDecimal("0");

        // 5 遍历所有的购物项 一个购物项就是一个订单明细 组装订单明细数据 批量保存订单明细
        List<OrderDetail> orderDetailList = new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(UUIDUtils.getUUIDOrderId().longValue()); // 订单项目的主键
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(orderId);
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());
            BigDecimal amount = shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber() + ""));
            totalAmount = totalAmount.add(amount);
            orderDetailList.add(orderDetail);
        }

        // 6 组装订单数据 批量保存订单数据
        orders.setId(orderId);
        orders.setNumber(String.valueOf(orderId));
        orders.setStatus(1);
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(totalAmount);
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());

        // 7 插入订单
        orderMapper.save(orders);

        // 8 插入批量的订单项
        orderDetailMapper.saveBatch(orderDetailList);

        // 9 删除当前用户的购物车列表数据
        shoppingCartMapper.clean(userId);


    }
}
